export default class Route{
	private timer:any = null;//定时器
	private unLogin = ():void=>{//处理未登录行为
		this.go({url:"/center/login/index"});
	}
	
	/*
	* 跳转
	* param		url			跳转链接		""
	* param		delay		延迟时长		0
	* param		message		提示信息		""
	* param		login		需要登录		false
	* param		disabled	是否禁用		false
	* param		success		成功回调		()=>{}
	*/
	go = ({
		url = "",
		delay = 0,
		message = "",
		login = true,
		disabled = false,
		success = ()=>{}
	}:{
		url:string,
		delay?:number,
		message?:string,
		login?:boolean,
		disabled?:boolean,
		success?:Function
	}):void=>{
		if(disabled)return
		if(!login){
			this.unLogin();
		}else{
			if(message.length > 0)uni.showToast({title:message,mask:true,icon:"none",duration:delay});
			this.timer = setTimeout(()=>{
				uni.navigateTo({url})
				success();
			},delay)
		}
	}
	
   /*
   * 返回
   * param		delta		返回层数		1
   * param		delay		延迟时长		0
   * param		message		提示信息		""
   * param		login		需要登录		false
   * param		disabled	是否禁用		false
   * param		success		成功回调		()=>{}
   */
	back = ({
		delta = 1,
		delay = 0,
		message = "",
		login = true,
		disabled = false,
		success = ()=>{}
	}:{
		delta?:number,
		delay?:number,
		message?:string,
		login?:boolean,
		disabled?:boolean,
		success?:Function
	}):void=>{
		if(disabled)return
		if(!login){
			this.unLogin();
		}else{
			if(message.length > 0)uni.showToast({title:message,mask:true,icon:"none",duration:delay});
			this.timer = setTimeout(()=>{
				uni.navigateBack({delta})
				success();
			},delay)
		}
	}
	
   /*
   * 返回后跳转
   * param		url			跳转链接		""
   * param		delta		返回层数		1
   * param		delay		延迟时长		0
   * param		message		提示信息		""
   * param		login		需要登录		true
   * param		disabled	是否禁用		false
   * param		success		成功回调		()=>{}
   */
	backTo = ({
		url = "",
		delta = 1,
		delay = 0,
		message = "",
		messageIcon = "none",
		login = true,
		disabled = false,
		success = ()=>{}
	}:{
		url: string
		delta?: number
		delay?: number
		message?: string
		messageIcon?: 'error' | 'exception' | 'fail' | 'loading' | 'none' | 'success'
		login?: boolean,
		disabled?: boolean,
		success?: Function
	}):void=>{
		if(disabled)return
		if(!login){
			this.unLogin();
		}else{
			if(message.length > 0)uni.showToast({title:message,mask:true,icon:messageIcon,duration:delay});
			this.timer = setTimeout(()=>{
				uni.navigateBack({delta,animationType:"none"})
				setTimeout(()=>{
					uni.navigateTo({url})
					success();
				},100)
			},delay)
		}
	}
	
   /*
   * 重定向
   * param		url			跳转链接		""
   * param		delay		延迟时长		0
   * param		message		提示信息		""
   * param		login		需要登录		true
   * param		disabled	是否禁用		false
   * param		success		成功回调		()=>{}
   */
	redirect = ({
		url = "",
		delay = 0,
		message = "",
		messageIcon = "none",
		login = true,
		disabled = false,
		success = ()=>{}
	}:{
		url: string
		delay?: number
		message?: string
		messageIcon?: 'error' | 'exception' | 'fail' | 'loading' | 'none' | 'success'
		login?: boolean
		disabled?: boolean
		success?: () => void
	}):void=>{
		if(disabled)return
		if(!login){
			this.unLogin();
		}else{
			if(message.length > 0)uni.showToast({title:message,mask:true,icon:messageIcon,duration:delay});
			this.timer = setTimeout(()=>{
				uni.redirectTo({url})
				success();
			},delay)
		}
		
	}

   /*
   * 关闭所有跳转到指定页面
   * param		url			跳转链接		""
   * param		disabled	是否禁用		false
   * param		success		成功回调		()=>{}
   */
	reLaunch = ({
		url = "",
		delay = 0,
		message = "",
		messageIcon = "none",
		login = true,
		disabled = false,
		success = ()=>{}
	}:{
		url:string,
		delta?: number
		delay?: number
		message?: string
		messageIcon?: 'error' | 'exception' | 'fail' | 'loading' | 'none' | 'success'
		login?: boolean,
		disabled?: boolean,
		success?: () => void
	}):void=>{
		if(disabled)return
		if(!login){
			this.unLogin();
		}else{
			if(message.length > 0)uni.showToast({title:message,mask:true,icon:messageIcon,duration:delay});
			this.timer = setTimeout(()=>{
				uni.reLaunch({url,success:()=>{
					success();
				}})
			},delay)
		}
	}

   /*
   * 自定义
   * param		delay		延迟时长		0
   * param		message		提示信息		""
   * param		login		需要登录		true
   * param		disabled	是否禁用		false
   * param		custom		自定义方法	()=>{}
   * param		success		成功回调		()=>{}
   */
	custom = ({
		delay = 0,//延迟时长
		message = "",//是否提示信息
		login = true,//是否登录
		disabled = false,//是否禁用
		Function = ()=>{},//自定义方法
		success = ()=>{}//成功方法
	}:{
		delay?:number,
		message?:string,
		login?:boolean,
		disabled?:boolean,
		Function?:Function,
		success?:Function
	}):void=>{
		if(disabled)return
		if(!login){
			this.unLogin();
		}else{
			if(message.length > 0)uni.showToast({title:message,mask:true,icon:"none",duration:delay});
			this.timer = setTimeout(()=>{
				Function();
				success();
			},delay)
		}
	}
	
	/*
	* 清除定时器
	*/
	clearTimer = ():void=>{
		if(this.timer)clearTimeout(this.timer);
	}
}